/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AutomationSelectionDialog.java
 *
 * Created on 12 avr. 2011, 21:10:40
 */

package modbuspal.automation;

import modbuspal.toolkit.FileTools;

import java.awt.Image;
import java.awt.Toolkit;

/**
 * A dialog for the user to select an automation from a list
 * @author nnovic
 */
public class AutomationSelectionDialog
extends javax.swing.JDialog
{
    private final AutomationListModel automationListModel;
    private boolean isOK = false;

    /** Creates new form AutomationSelectionDialog 
     * @param al the list of automations to display in the form
     */
    public AutomationSelectionDialog(AutomationListModel al)
    {
        this(al,null);
    }

    /**
     *  Creates new form AutomationSelectionDialog 
     * @param a the list of automatoins to display in the form
     */
    public AutomationSelectionDialog(Automation[] a)
    {
        this( new AutomationListModel(a), null );
    }

    /**
     *  Creates new form AutomationSelectionDialog, and
     *  specifies which automation should be selecte 
     *  by default.
     * @param a the list of automations to display
     * @param selected the selected automation
     */
    public AutomationSelectionDialog(Automation[] a, Automation selected)
    {
        this( new AutomationListModel(a), selected );
    }

    /**
     *  Creates new form AutomationSelectionDialog, and
     *  specifies which automation should be selecte 
     *  by default.
     * @param al the list of automations to display
     * @param selected the selected automation
     */
    public AutomationSelectionDialog(AutomationListModel al, Automation selected)
    {
        super();
        setModalityType(ModalityType.APPLICATION_MODAL);
        automationListModel = al;
        initComponents();
        setIconImage(FileTools.getImage("/modbuspal/main/img/icon32.png"));
        select(selected);
    }


    private void select(Automation selected)
    {
        if(selected==null)
        {
            return;
        }

        jList1.setSelectedValue( selected.getName(), true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select an automation");

        jList1.setModel(automationListModel);
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        isOK=true;
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed


    /**
     * Returns the selected automation, or null if no selection
     * has been made, or null if the user has no validated the selection by 
     * clicking on OK.
     * @return the selected automation
     */
    public Automation getSelectedAutomation()
    {
        if(isOK==false)
        {
            return null;
        }

        int i = jList1.getSelectedIndex();
        return automationListModel.getAutomation(i);
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

}
